<?php
namespace BoxAdmin\Controller;

use Think\Controller;

class StoreController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->store = D('System');
        $this->wmenu = D('Wmenu');
        $this->slide = BoxModel('Slide');
        $this->nav = D('Nav');
        $this->payment = D('Payment');
        $this->give = BoxModel('Give');
        $this->id = I('id', '', 'intval');
        $this->kw = I('get.kw');
        $this->store_id = I('store_id', 1);

    }

    /**
     * 商家列表
     */
    public function index()
    {
        $this->memuNav = "index";
        $store = BoxModel('Store');
        $where = 'isOpen=1';
        if (!empty($this->kw)) {
            $where .= ' and (store_name like "%' . $this->kw . '%" or photo like "%' . $this->kw . '%" or domain like "%' . $this->kw . '%")';
        }
        $order = 'store_id desc';
        $this->setPager($store, 12, $where);
        $this->list = $store->where($where)->order($order)->limit($this->limit)->select();
        if ($this->ids = I('ids')) {
            $this->display("copy_store");
        } else {
            $this->display();
        }
        //dump($store);

    }

    /**
     * [add 添加或修改微信帐号页]
     */
    public function setting()
    {
        $this->memuNav = "index";
        $this->store_id = I('store_id');
        if (!empty($this->store_id)) {
            $this->data = $this->store->editWechat($this->store_id);
        }
        $this->Token = $_GET['storeDomain'];
        $this->display();
    }

    /*****中转页面设置****/
    function redirectImage()
    {
        $this->memuNav = "redirectImage";
        if ($_POST) {
            $info = $this->_upload("/redirectImage/");

            if ($info) {
                $row['redirectImage'] = $info['img']['savepath'] . $info['img']['savename'];
                // dump($row);
            }

            D('SystemStore')->where("store_id='$this->store_id'")->save($row);
        }
        $this->data = $this->store->editWechat($this->store_id);
        $this->display();
        // dump( $this->data);

    }

    /**
     * [checkwechat 检测微信号是否重复]
     * @return [type] [description]
     */
    public function checkwechat()
    {
        $store_wechat = I('store_wechat');
        $check = BoxModel('Store')->where("store_wechat='$store_wechat' and store_id<>'$this->store_id'")->find();
        if ($check) {
            echo 'false';
        } else {
            echo 'true';
        }
    }

    /**
     * [wechatInsert 添加或修改微信帐号方法]
     *
     */
    public function wechatInsert()
    {
        if (IS_POST) {
            $arr = array(
                'store_name' => I('store_name'),
                'address' => I('address'),
                'photo' => I('photo'),
                'domain' => I("domain"),
                'copyright' => I("copyright"),
            );
            if ($this->store_id) {
                D('SystemStore')->where("store_id='$this->store_id'")->save($arr);
            }
            D('Log')->addLogs(session('user_name'), '编辑店铺');
            $this->jump("操作成功");

        }
    }

    /**
     * [wechatDel 删除微信帐号]
     * @return [type] [description]
     */
    public function wechatDel()
    {

        $del = $this->store->delWechat($this->id);
        if ($del) {
            $this->redirect('index');
            D('Log')->addLogs(session('user_name'), '删除微信帐号');
        } else {
            $this->error('微信帐号删除失败！');
        }
    }


    /**
     * [wechatMenu 微信菜单页]
     * tree() 格式化
     * @return [type] [description]
     */
    public function wechatMenu()
    {
        $this->memuNav = "wechatMenu";
        $this->data = $this->wmenu->tree("store_id='$this->store_id'");
        $this->display();
    }

    /**
     * [wmenuAdd 添加 修改微信菜单页]
     * @return [type] [description]
     */
    public function wmenuAdd()
    {
        $this->memuNav = "wechatMenu";
        if (!empty($this->id)) {
            $this->list = $this->wmenu->editMenu($this->id);
        }
        $this->data = $this->wmenu->tree();
        $this->display();
    }

    /**********发布微信菜单接口********/
    public function wmenuCreate()
    {
        $wechatconfig = D('SystemStore')->where("store_id='$this->store_id'")->field("appID,appsecret")->find();
        if ($wechatconfig['appID'] && $wechatconfig['appsecret']) {
            $memudata = BoxModel("wechat_menu")->where("store_id='$this->store_id' and pid=0 and status=0")->field("id,type,name,url")->order("sort_order")->limit(3)->select();
            // if(count($memudata)!=3)$this->error('不够3个一级菜单！！！！');
            /******信菜单类型****/
            foreach ($memudata as $key => $one) {
                if ($one['type'] == 'click') {
                    $memudata[$key]['key'] = $one['url'];
                    unset($memudata[$key]['url']);
                }
            }
            $wecatMemu = array();
            /******************生成信菜单配置***************/
            foreach ($memudata as $key => $one) {
                $sub_button = array();
                $pid = $one['id'];
                $sub_button = BoxModel("wechat_menu")->where("pid='$pid' and status=0")->order("sort_order")->field("type,name,url")->limit(5)->select();
                /******信菜单类型****/
                foreach ($sub_button as $k => $one1) {
                    if ($one1['type'] == 'click') {
                        $sub_button[$k]['key'] = $one1['url'];
                        unset($sub_button[$k]['url']);
                    }
                }
                if ($sub_button) {
                    $wecatMemu[$key]['name'] = $one['name'];
                    $wecatMemu[$key]['sub_button'] = $sub_button;
                } else {
                    unset($one['id']);
                    $wecatMemu[$key] = $one;
                }
            }

            /********************推送到微信**************************/
            $weiXin = new \Common\Org\weiXin($wechatconfig);
            $r = $weiXin->createMemu($wecatMemu);
            $data = json_decode($r, true);
            if ($data['errmsg'] == "ok")
                $this->error('配置成功！');
            else
                $this->error('生成失败！，错误信息：' . $data['errcode'] . ":" . $data['errmsg']);
        } else {
            $this->error('请先设置微信，appID、appsecret！');
        }
    }


    /**
     * [wmenuInsert 添加 修改微信菜单方法]
     * @return [type] [description]
     */
    public function wmenuInsert()
    {
        if (IS_POST) {
            if (empty($this->id)) {
                $data = array(
                    'pid' => I('post.pid', '', 'intval'),
                    'name' => I('post.name'),
                    'url' => I('post.url'),
                    'type' => I('post.type'),
                    'created' => time(),
                    'store_id' => $this->store_id,
                );
                if ($this->wmenu->addMenu($data)) {
                    D('Log')->addLogs(session('user_name'), '添加微信帐号');
                    $this->redirect('wechatMenu');
                } else {
                    $this->error('添加微信菜单失败！');
                }
            } else {
                $arr = array(
                    'name' => I('post.name'),
                    'url' => I('post.url'),
                    'type' => I('post.type'),
                    'status' => I('status'),
                );
                $this->wmenu->upMenu($this->id, $arr);
                D('Log')->addLogs(session('user_name'), '修改微信帐号');
                $this->redirect('wechatMenu');
            }
        }
    }

    /**
     * [upMsort 更新微信菜单排序]
     * @return [type] [description]
     */
    public function upMsort()
    {
        foreach ($_POST as $id => $sort) {
            BoxModel('Wechat_menu')->where(array('id' => $id))->setField('sort', $sort);
        }
        D('Log')->addLogs(session('user_name'), '微信菜单排序');
        $this->redirect('wechatMenu');
    }

    /**
     * [wmenuDel 删除微信菜单]
     * @return [type] [description]
     */
    public function wmenuDel()
    {
        $count = BoxModel('Wechat_menu')->where(array("pid" => $this->id))->count();
        if ($count > 0) {
            $this->error("该菜单下还有子菜单，无法删除！");
        }
        if ($this->wmenu->delMenu($this->id)) {
            D('Log')->addLogs(session('user_name'), '删除微信菜单');
            $this->redirect('wechatMenu');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * [slide 网站轮播图片首页]
     * @return [type] [description]
     */
    public function  slide()
    {
        $this->memuNav = "slide";
        $where = 'store_id=' . $this->store_id;
        $order = 'sort_order,id desc';
        $type = new \Common\Org\Type();
        $this->type = $type->getChilds('图片显示位置');
        //dump( $this->type);
        if ($this->position = I('position')) {
            $where = "position='$this->position'";
        }
        $this->setPager($this->slide, 8, $where);
        $this->list = $this->slide->where($where)->order($order)->limit($this->limit)->select();
        $this->display();
    }


    /**
     * [slideDel 删除轮播图片方法]
     * @return [type] [description]
     */
    public function slideDel()
    {
        if ($this->slide->where('id=' . $this->id)->delete()) {
            D('Log')->addLogs(session('user_name'), '删除轮播图片');
            $this->jump("删除成功");
        } else {
            $this->error('轮播图页删除失败！');
        }
    }

    /**
     * [upSsort 更新轮播图排序]
     * @return [type] [description]
     */
    public function upSsort()
    {
        foreach ($_POST as $id => $sort) {
            $this->slide->where(array('id' => $id))->setField('sort', $sort);
        }
        D('Log')->addLogs(session('user_name'), '更新轮播图排序');
        $this->jump("成功");
    }

    /**
     * [slideEdit 修改轮播图片页]
     * @return [type] [description]
     */
    public function slideEdit()
    {
        $this->memuNav = "slide";
        $type = new \Common\Org\Type();
        $this->type = $type->getChilds('图片显示位置');
        $this->data = $this->slide->where('id=' . $this->id)->find();
        if ($this->data) {
            $this->store_id = $this->data['store_id'];
        }
        $this->display();
    }

    /**
     * [slideUp 修改轮播图片行为]
     * @return [type] [description]
     */
    public function slideUp()
    {
        if (IS_POST) {
            $info = $this->_upload("/slide/");
            if ($info) {
                $data['img'] = $info['img']['savepath'] . $info['img']['savename'];
            }
            $id = I('id');
            $data['title'] = I('post.title');
            $data['url'] = (I('post.url'));
            $data['position'] = I('post.position');
            $data['status'] = I('post.status');
            $data['info'] = I('post.info');
            if ($id) {
                $this->slide->where("id=$id")->save($data);
                D('Log')->addLogs(session('user_name'), '修改轮播图片');
            } else {
                $data['store_id'] = $this->store_id;
                $data['created'] = time();
                $this->slide->add($data);
                D('Log')->addLogs(session('user_name'), '添加轮播图片');
            }
            $this->redirect('slide', array('store_id' => $this->store_id));

        }
    }

    /**
     * [navar 网站导航首页]
     * @return [type] [description]
     */
    public function nav()
    {
        $this->memuNav = "nav";
        $this->list = $this->nav->tree('store_id=' . $this->store_id);
        $this->display();
    }

    /**
     * [navAdd 添加 修改网站导航页]
     * @return [type] [description]
     */
    public function navAdd()
    {
        $this->memuNav = "nav";
        if ($this->id) {
            $this->list = $this->nav->editNav($this->id);
        }
        $this->data = $this->nav->tree();
        $this->display();
    }

    /**
     * [navInsert 添加  修改导航行为]
     * @return [type] [description]
     */
    public function navInsert()
    {
        if (IS_POST) {
            $data = array(
                'pid' => I('post.pid', '', 'intval'),
                'name' => I('post.name'),
                'url' => I('post.url'),
                'status' => I('post.status'),
                'store_id' => $this->store_id,
            );
            $info = $this->_upload("/nav/");
            if ($info['img']['savepath']) {
                $data['img'] = $info['img']['savepath'] . $info['img']['savename'];
            }

            if (empty($this->id)) {
                $data['created'] = time();
                if ($this->nav->addNav($data)) {
                    D('Log')->addLogs(session('user_name'), '添加导航');
                    $this->redirect('nav');
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->nav->upNav($this->id, $data);
                D('Log')->addLogs(session('user_name'), '修改导航');
                $this->redirect('nav');
            }
        }

    }

    /**
     * [navDel 删除导航行为]
     * @return [type] [description]
     */
    public function navDel()
    {
        $count = $this->nav->where(array("pid" => $this->id))->count();
        if ($count > 0) {
            $this->error("该菜单下还有子菜单，无法删除！");
        }
        if ($this->nav->delNav($this->id)) {
            D('Log')->addLogs(session('user_name'), '删除导航');
            $this->redirect('nav');
        }
    }

    /**
     * [upNavsort 更新导航排序行为]
     * @return [type] [description]
     */
    public function upNavsort()
    {
        foreach ($_POST as $id => $sort) {
            BoxModel('Nav')->where(array('id' => $id))->setField('sort', $sort);
        }
        D('Log')->addLogs(session('user_name'), '更新导航排序');
        $this->redirect('nav');
    }

    /**********运费设置**/
    public function freight()
    {
        $this->memuNav = 'freight';
        if (IS_POST) {
            $config = I('config');
            $freight_config = json_encode($config);
            $r = D('SystemStore')->where("store_id='1'")->save(array('freight_config' => $freight_config));
            //   dump(D('SystemStore'));
        }
        $data = D('SystemStore')->where("store_id='1'")->field("freight_config")->find();
        $this->feeconfig = json_decode($data['freight_config'], true);
        // dump($this->feeconfig);
        $this->area = BoxModel("region")->where("area_type=1")->select();
        $this->display();
    }

    public function city_area()
    {
        $pid = I('pid');
        $area = BoxModel("region")->where("(area_type=2 and parent_id='$pid' )or (area_id='$pid')")->order("area_id")->select();
        echo json_encode($area);
    }

}

?>